package com.theyeasy.dvmini.pc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.socket.TextMessage;
import org.testng.util.Strings;
import org.theyeasy.weixin.util.CusAccessObjectUtil;

import com.theyeasy.dvmini.config.PCLoginConfig;
import com.theyeasy.dvmini.dao.SysWxauthorizeDao;
import com.theyeasy.dvmini.model.SysWxauthorize;
import com.theyeasy.dvmini.pc.controller.BaseController;
import com.theyeasy.dvmini.session.PCSessionHelper;
import com.theyeasy.dvmini.session.PCSessionHelper.PCSessionName;
import com.theyeasy.dvmini.util.RedisUtil;
import com.theyeasy.dvmini.vo.Vo_msg;

@Controller
@RequestMapping("/")
public class CommonController extends BaseController {

	@Autowired
	SysWxauthorizeDao sysWxauthorizeDao;
	@Autowired
	PCLoginConfig config;

	/**
	 * 登录
	 * 
	 * @Description (TODO这里用一句话描述这个方法的作用)
	 * @param account
	 * @param pwd
	 * @return
	 */
	@RequestMapping("/loginIn")
	public @ResponseBody Vo_msg agentlogon(HttpServletRequest request,String account, String pwd) {
		if (Strings.isNullOrEmpty(account))
			return new Vo_msg(-1, "登录失败，账号不能为空");
		if (Strings.isNullOrEmpty(pwd))
			return new Vo_msg(-1, "登录失败，密码不能为空");
		
		
		//redis中授权公众号的appid为空时，从数据库中获取并设置它。add by lcc 2017-11-16
		String appid = RedisUtil.getMpAppid();
		if(null==appid || appid.length()<1)
		{
			SysWxauthorize auth = new SysWxauthorize();
			auth.setStatus(1);
			auth.setDeleted(false);
			
			auth = sysWxauthorizeDao.selectOne(auth);
			if(auth!=null)
			{
				appid = auth.getWxappid();
				if (null!=appid && appid.length()>0) RedisUtil.setMpAppid(auth.getWxappid());								
			}
		}
		
		// 获取配置的用户名和密码
		if (account.equals(config.getAccount()) && pwd.equals(config.getPwd())) {
			// 保存账号信息
			PCSessionHelper.SetSession(PCSessionName.Account, account);
			return new Vo_msg(0, "ok");
		} else {
			return new Vo_msg(-1, "登录失败，用户名或密码错误");
		}
	}

	/**
	 * 退出登录
	 * 
	 * @return
	 */
	@RequestMapping(value = "exit")
	public @ResponseBody Object exit() {
		PCSessionHelper.removeBrowser();
		return new Vo_msg(0, "ok");
	}

	@RequestMapping(value = "/game1", method = RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request, Map<String, String> map) {

		ModelAndView modelAndView = new ModelAndView("/pc/game1");
		return modelAndView;
	}

	@RequestMapping(value = "/game2", method = RequestMethod.GET)
	public String index2() {
		return "/pc/game2";
	}

	@RequestMapping(value = "/game3", method = RequestMethod.GET)
	public String index3() {
		return "/pc/game3";
	}

	@RequestMapping(value = "/controller", method = RequestMethod.GET)
    public String controller() {
        return "/UE/controller";
    }
	
	/*
	 * @RequestMapping(value = "/send", method = RequestMethod.GET) public void
	 * send(@RequestParam(value = "uid", required = false, defaultValue = "all")
	 * String uid) { TextMessage message = new TextMessage("aaaa".getBytes()); if
	 * ("all".equals(uid)) { PcWebSocketHandler.sendMessageToAll(message, "1"); }
	 * else { PcWebSocketHandler.sendToOne(message, "1", uid); }
	 * 
	 * }
	 */
	//测试富文本编辑器
	@RequestMapping(value = "/test", method = RequestMethod.GET)
    public ModelAndView test(HttpServletRequest request, Map<String, String> map) {

        ModelAndView modelAndView = new ModelAndView("/pc/testue");
        return modelAndView;
    }

	

}